package gui.Packaging;

import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JScrollPane;

import clients.RPCClient;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.SystemColor;

import javax.swing.border.TitledBorder;

import java.awt.Font;
/**
 * 
 * @author adam 
 *Packaging main panel where it holds the list of orders need to be packed .
 */
public class PackagingMainPanel extends JPanel{
	private JTable table;
	private DefaultTableModel model ;
	private int rowSetected;
	private int columnSelected;
	private RPCClient myPackagingCleint;
	
/**
 * Constructor	
 * @param frame-PackagingFrame
 */
	public PackagingMainPanel(PackagingFrame frame) {
		setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setBackground(new Color(173, 216, 230));
		
		setLayout(new MigLayout("", "[][][]", "[][][grow]"));
		
		JLabel label = new JLabel("מחלקת - אריזה");
		label.setFont(new Font("Tahoma", Font.PLAIN, 13));
		add(label, "cell 1 0,alignx center");
		
		JLabel label_2 = new JLabel("דיווח");
		label_2.setFont(new Font("Tahoma", Font.PLAIN, 13));
		add(label_2, "cell 0 1,alignx center");
		
		JLabel label_1 = new JLabel("ניהול ");
		label_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		add(label_1, "cell 2 1,alignx center");
		
		JButton button_1 = new JButton("דווח");
		button_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
		add(button_1, "cell 0 2,aligny top");
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
				rowSetected = table.getSelectedRow();
				columnSelected = table.getSelectedColumn();

			}
		});
		add(scrollPane, "cell 1 2,alignx center");
		
		table = new JTable();
		table.setFont(new Font("Tahoma", Font.PLAIN, 15));
		scrollPane.setViewportView(table);
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u05DE\u05E1\u05E4\u05E8 \u05E4\u05E7\"\u05E2"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		model = (DefaultTableModel) table.getModel();
		JButton button = new JButton("נהל");
		button.setFont(new Font("Tahoma", Font.PLAIN, 13));
		add(button, "cell 2 2,alignx center,aligny top");

		//Part of the Initializing proccess :
		try{
			ArrayList<Object>message = new ArrayList<>();
			ArrayList<Object>respond = new ArrayList<>();
			myPackagingCleint = new RPCClient();
			message.add("initializePackageMailPanelVariables");
			respond = myPackagingCleint.call(message);
			if(respond.get(1).toString().equalsIgnoreCase("valid"))
			{
				setVairables(respond);
			}
			else
			{
				// Failed to get list
        		JOptionPane.showMessageDialog(frame,
					    "שגיאה בחיבור לשרת","שגיאה",JOptionPane.ERROR_MESSAGE);
			//	System.out.println("no GOOD");
			}
		}catch(Exception e)
		{
			//e.printStackTrace();
		}
	}
/**
 * 
 * @param respond-initializePackageMailPanelVariables - a function in mysql server- adds the orderID's into the main table , orders that came from earlier stages .
 */
	public void setVairables(ArrayList<Object> respond) {
		
	Iterator<Object>itr = respond.iterator();
	itr.next();itr.next(); // passed the first 2 non needed values.
	while(itr.hasNext())
	{
		model.addRow(new Object[]{itr.next().toString()});
	}
	}

	@Override
	public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Color color1 = getBackground();
    Color color2 = color1.darker();
    int w = getWidth();
    int h = getHeight(); 
    GradientPaint gp = new GradientPaint(
            0, 0, color1,
            0, h, color2);

    g2d.setPaint(gp);
    g2d.fillRect(0, 0, w, h);
    }
}
